<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">考勤班次</block>
		</cu-custom>
		<radio-group @change="radioChange" class="cu-list menu margin-top sm-border">
			<navigator url="shifts-add" class="cu-item">
				<view class="action text-lg">
					<text class="cuIcon-roundaddfill text-blue"></text><text class="margin-left-xs">新增班次</text>
				</view>
			</navigator>
			<view v-for="(item,sindex) in shifts" class="cu-item">
				<view class="content">
					<label>
						<radio class="round blue" :checked="index==sindex" :value="sindex.toString()"></radio>
						<text class="text-grey padding-left">{{item.sname}}</text>
						<text class="text-grey">({{item.stime}}-{{item.etime}}{{item.stime2!=''?' ...':''}})</text>
					</label>
				</view>
				<view class="action text-lg">
					<button @click="modifyShifts" :data-index="sindex" type="" class="cuIcon-edit text-gray bg-white"></button>
				</view>
			</view>
		</radio-group>
		<view style="height: 160upx"></view>
		<view class="bottom response">
			<button @click="saveSelect" :disabled="loadding" style="background-color: #5599FF;" :loading="loadding" type="primary">保存</button>
		</view>
	</view>
</template>

<script>
	let index=0
	export default {
		data() {
			return {
				shifts: [],
				index,
				loadding: false,
				selectSname:''
			}
		},
		onShow() {
			this.getShifts()
		},
		onLoad(options) {
			if(typeof(options.sname)!='undefined'){
				this.selectSname=options.sname
			}
		},
		methods: {
			radioChange: function(e) {
				index = e.target.value
				this.index = index
			},
			modifyShifts(e){
				// console.log(e)
				let index = e.currentTarget.dataset.index
				let data = JSON.stringify(this.shifts[index]) // result传递的query参数。我们转为string
				this.$router.push({
					path: 'shifts-add',
					query: {
						res: data,
					}
				})
			},
			saveSelect(){
				let shift = this.shifts[this.index]
				let pages = getCurrentPages()
				// console.log(pages)
				let prePage = pages[pages.length - 2]
				prePage.selectShift = shift
				uni.navigateBack({
					delta: 1
				})
			},
			getShifts() {
				let that = this
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_SHIFTS_LIST,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						unitno: getApp().globalData.orgcode
					},
					success: (res) => {
						let data = res.data.data
						if (res.data.code == 0 && typeof(data) != 'undefined') {
							if(that.selectSname!=''){
								data.forEach(function(item,index){
									if(that.selectSname==item.sname){
										that.index = index
									}
								})
							}
							that.shifts = data
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				});
			}
		}
	}
</script>

<style>
	.cu-add {
		width: 40upx;
		height: 40upx;
	}

	.action button::after {
		border: none;
	}

	.bottom {
		background-color: #FFFFFF;
		padding: 20upx;
		position: fixed;
		bottom: 0;
	}
</style>
